﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Atlandiz.Data;
using System.IO;
using FastReport;
using FastReport.Data;
using FastReport.Preview;
using FastReport.Utils;


namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    public partial class ListeCihaz : DevExpress.XtraEditors.XtraForm
    {
        public Report FReport = new Report();
        public bool ForSelection = false;
        DataTable FormDataTable;

        public ListeCihaz()
        {
            InitializeComponent();
        }

        private void XtraForm1_Load(object sender, EventArgs e)
        {
            Army.EventConfirm = false;
            CihazListele();
        }


        public void CihazListele()
        {
            try
            {
                string SorguEk = "";
                if (ShowHurda.Checked)
                {
                    SorguEk = "";
                }
                else
                {
                    SorguEk = " and c.isHurda=0 ";
                }

                if (Army.UserTipi != 1)
                {
                    SorguEk += " AND c.DepoId IN (select id_depolar from KullaniciDepo where id_kullanici=" + Army.ActiveUserId + ") ";
                }


                string Sorgu = "Select \n" +
                    "c.cID [Cihaz ID],\n" +
                    "ct.ChzTur [Cihaz Türü],\n" +
                    "cm.ChzMarka [Marka],\n" +
                    "cmd.ChzModel [Model],\n" +
                    "c.SeriNoIMEI [Seri veya Imei],\n" +
                    "c.ServisTag [Servis Tag],\n" +
                    "c.Etiket [Etiket],\n" +
                    "FORMAT(c.cAlimTarihi,'dd.MM.yyyy') [Alış Tarihi],\n" +
                    "c.Aciklama [Açıklama],\n" +
                    "t.tFirmaUnvan [Tedarikçi],\n" +
                    "c.Maliyet [Maliyet],\n" +
                    "c.ZimmetDegeri [Zimmet Değeri],\n" +
                    "(Select DepoAdi from Depolar where id=c.DepoId) [Depo Adı]\n" +
                    "	from Cihaz c \n" +
                    "	left join CihazTur ct on c.ctID=ct.ctID \n" +
                    "	left join CihazMarka cm on c.cmID=cm.cmID \n" +
                    "	left join CihazModel cmd on c.cmdID=cmd.cmdID \n" +
                    "	left join Tedarikci t on c.tID=t.tID\n" +
                "where c.isDeleted=0 " + SorguEk + " ORDER BY c.cID Desc";

                DataTable dt = Army.SQL().GetDataTable(Sorgu);

                _listviewCihazlar.Items.Clear();

                foreach (DataRow item in dt.Rows)
                {
                    ListViewItem ls = new ListViewItem(item[0].ToString());
                    ls.SubItems.Add(item[1].ToString());
                    ls.SubItems.Add(item[2].ToString());
                    ls.SubItems.Add(item[3].ToString());
                    ls.SubItems.Add(item[4].ToString());
                    ls.SubItems.Add(item[5].ToString());
                    ls.SubItems.Add(item[6].ToString());
                    ls.SubItems.Add(item[7].ToString());
                    ls.SubItems.Add(item[7].ToString());
                    ls.SubItems.Add(item[8].ToString());
                    ls.SubItems.Add(item[9].ToString());
                    ls.SubItems.Add(item[10].ToString());
                    ls.SubItems.Add(item[11].ToString());

                    _listviewCihazlar.Items.Add(ls);
                }

                FormDataTable = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

            
        }
      
        private void btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void KDuzenlexxx()
        {
            FormCihaz c = new FormCihaz();
            c.KayID = int.Parse(_listviewCihazlar.SelectedItems[0].SubItems[0].Text);
            c.ShowDialog();
            CihazListele();
        }
        private void düzenleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_listviewCihazlar.SelectedItems.Count > 0)
            {
                KDuzenlexxx();
            }
            else
            {
                MessageBox.Show("Lütfen, düzenlenecek kaydı seçiniz...");
            }
        }

        private void silToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_listviewCihazlar.SelectedItems.Count > 0)
            {
                PopupAreYouSure SilOnay = new PopupAreYouSure();
                Army.EventMessage = "Seçilen Cihaz Silinecek,\nEmin misiniz?";
                SilOnay.ShowDialog();

                if (Army.EventConfirm)
                {
                    Army.SQL().cmd("Update Cihaz SET isDeleted=1 where cID=" + _listviewCihazlar.SelectedItems[0].SubItems[0].Text.ToString());
                    MessageBox.Show("Seçilen Cihaz Silindi.", "Kayıt Silindi.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    CihazListele();
                    //sil komutu
                }
            }
        }

        private void simpleButton3_Click_1(object sender, EventArgs e)
        {
            CihazListele();
        }

     
        private void listView1_DoubleClick_1(object sender, EventArgs e)
        {
            if (_listviewCihazlar.SelectedItems.Count > 0 & ForSelection)
            {
                Army.SecilenID = int.Parse(_listviewCihazlar.SelectedItems[0].SubItems[0].Text);
                Army.EventConfirm = true;
                this.Close();
            }
        }

        private void _btnListeGuncelle_Click(object sender, EventArgs e)
        {
            CihazListele();
        }

        private void _btnYeni_Click(object sender, EventArgs e)
        {
            FormCihaz c = new FormCihaz();
            c.ShowDialog();
            CihazListele();
        }

        private void _btnAra_Click(object sender, EventArgs e)
        {
            string SorguEk = "";
            if (ShowHurda.Checked)
            {
                SorguEk = "";
            }
            else
            {
                SorguEk = " and c.isHurda=0 ";
            }

                string Sorgu = "Select \n" +
               "c.cID [Cihaz ID],\n" +
               "ct.ChzTur [Cihaz Türü],\n" +
               "cm.ChzMarka [Marka],\n" +
               "cmd.ChzModel [Model],\n" +
               "c.SeriNoIMEI [Seri veya Imei],\n" +
               "c.ServisTag [Servis Tag],\n" +
               "c.cAlimTarihi [Alış Tarihi],\n" +
               "c.Aciklama [Açıklama],\n" +
               "t.tFirmaUnvan [Tedarikçi],\n" +
               "c.Maliyet [Maliyet],\n" +
               "c.ZimmetDegeri [Zimmet Değeri],\n" +
               "(Select DepoAdi from Depolar where id=c.DepoId) [Depo Adı]\n" +
               "	from Cihaz c \n" +
               "	left join CihazTur ct on c.ctID=ct.ctID \n" +
               "	left join CihazMarka cm on c.cmID=cm.cmID \n" +
               "	left join CihazModel cmd on c.cmdID=cmd.cmdID \n" +
               "	left join Tedarikci t on c.tID=t.tID\n" +
           "where c.isDeleted=0 " + SorguEk + " \n" +
           "and c.cID like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or ct.ChzTur like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or cm.ChzMarka like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or cmd.ChzModel like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or c.SeriNoIMEI like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%'  or c.ServisTag like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or c.Aciklama like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or t.tFirmaUnvan like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or c.Maliyet like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or c.ZimmetDegeri like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or (Select DepoAdi from Depolar where id=c.DepoId) like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%'  ";


                DataTable dt = Army.SQL().GetDataTable(Sorgu);

                _listviewCihazlar.Items.Clear();

                foreach (DataRow item in dt.Rows)
                {
                    ListViewItem ls = new ListViewItem(item[0].ToString());
                    ls.SubItems.Add(item[1].ToString());
                    ls.SubItems.Add(item[2].ToString());
                    ls.SubItems.Add(item[3].ToString());
                    ls.SubItems.Add(item[4].ToString());
                    ls.SubItems.Add(item[5].ToString());
                    ls.SubItems.Add(item[6].ToString());
                    ls.SubItems.Add(item[7].ToString());
                    ls.SubItems.Add(item[8].ToString());
                    ls.SubItems.Add(item[9].ToString());
                    ls.SubItems.Add(item[10].ToString());
                    ls.SubItems.Add(item[11].ToString());

                    _listviewCihazlar.Items.Add(ls);
                }
                _txtAraString.Focus();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {

            if (_listviewCihazlar.SelectedItems.Count > 0)
            {
                ListeNesneler NesnelerListe = new ListeNesneler();
                NesnelerListe.Urun_Id = Convert.ToInt32(_listviewCihazlar.SelectedItems[0].SubItems[0].Text);
                NesnelerListe.UrunTipi=1;
                NesnelerListe.ShowDialog();

            }
            else
            {
                MessageBox.Show("Listeden bir ürün seçmelisiniz.");
            }

        }
        private void simpleButton2_Click(object sender, EventArgs e)
        {

            Random rnd = new Random();
            int DosyaAdEk = rnd.Next(99999999);

            saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            saveFileDialog1.Filter = "Excel Dökümanı|*.xlsx";
            saveFileDialog1.Title = "Excel Aktarım";
            saveFileDialog1.FileName="Cihazlar-" + DosyaAdEk;
            saveFileDialog1.ShowDialog();
            
            //if (saveFileDialog1.FileName != "")
            //{
            //    Army.MakeExcelFromDatatable(FormDataTable, saveFileDialog1.FileName);
            //}
            //MessageBox.Show(saveFileDialog1.FileName + "\nDosya Aktarıldı.", "İşlem Tamam.", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        
       private void simpleButton3_Click(object sender, EventArgs e)
        {
            DataTable forReport = new DataTable();

            forReport = FormDataTable;
            forReport.TableName = "Datalar";
            FReport.RegisterData(forReport, "Datalar");
            FReport.GetDataSource("Datalar").Enabled = true;

            if (Army.ReportForYazilim.Length > 0 & File.Exists(Army.ProgramBaseEnvironmentPath + Army.ReportForCihazListe))
            {
                FReport.Load(Army.ProgramBaseEnvironmentPath + Army.ReportForCihazListe);
            }
            FReport.Design();
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            DataTable forReport = new DataTable();

            forReport = FormDataTable;
            forReport.TableName = "Datalar";
            FReport.RegisterData(forReport, "Datalar");
            FReport.GetDataSource("Datalar").Enabled = true;

            if (Army.ReportForYazilim.Length > 0 & File.Exists(Army.ProgramBaseEnvironmentPath + Army.ReportForCihazListe))
            {
                FReport.Load(Army.ProgramBaseEnvironmentPath + Army.ReportForCihazListe);
            }
            FReport.Show();
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            DataTable forReport = new DataTable();

            forReport = FormDataTable;
            forReport.TableName = "Datalar";
            FReport.RegisterData(forReport, "Datalar");
            FReport.GetDataSource("Datalar").Enabled = true;

            if (Army.ReportForYazilim.Length > 0 & File.Exists(Army.ProgramBaseEnvironmentPath + Army.ReportForCihazListe))
            {
                FReport.Load(Army.ProgramBaseEnvironmentPath + Army.ReportForCihazListe);
            }
            FReport.Print();
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            if (saveFileDialog1.FileName != "")
            {
                Army.MakeExcelFromDatatable(FormDataTable, saveFileDialog1.FileName);
            }
            MessageBox.Show(saveFileDialog1.FileName + "\nDosya Aktarıldı.", "İşlem Tamam.", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {

            if (_listviewCihazlar.SelectedItems.Count > 0)
            {
                ListeServisBakim ServisListe = new ListeServisBakim();
                ServisListe.Urun_Id = Convert.ToInt32(_listviewCihazlar.SelectedItems[0].SubItems[0].Text);
                ServisListe.UrunTipi = 1;
                ServisListe.ShowDialog();
            }
            else
            {
                MessageBox.Show("Listeden bir ürün seçmelisiniz.");
            }
        }

        

    }
}